package ca.concordia.soen6011.impl;

import java.util.Date;

import ca.concordia.soen6011.Result;
import ca.concordia.soen6011.commons.Helpers;
import ca.concordia.soen6011.exceptions.UndefinedOutputException;
import ca.concordia.soen6011.interfaces.MathFunction;

/**
 * Log[base e] (x) transient function
 * 
 * Calculates the Log[base e] (x)
 * 
 * @author Ammar, Amir
 * @since July-August 2014
 * @copyright TRANSIENT FUNCTION CALCULATOR is a team project for the SOEN 6011
 *            course delivered to Dr. Kamthan, Pankaj at Concordia University,
 *            Montreal for the summer 2014 session.
 */
public class LnX implements MathFunction {

	@Override
	public Result compute(double x, double precision)
			throws UndefinedOutputException {
		Result result = new Result();
		Date start = new Date();
		if (x <= 0)
			throw new UndefinedOutputException();
		double logex = 0.0;
		try {
			logex = Helpers.ln(x, precision);
		} catch (Exception e) {
			throw new UndefinedOutputException();
		}
		Date end = new Date();
		result.setPrecison(precision);
		result.setComputationTime(end.getTime() - start.getTime());
		result.setOutput(logex);
		return result;
	}

	@Override
	public String getName() {
		return "ln(x)";
	}

	@Override
	public void validate(double x, double precision) throws Exception {
		if (x <= 0)
			throw new Exception("argument cannot be negative or zero");
	}

}
